//
//  DPreferences.swift
//  Drop
//
//  Created by hushaohua on 2018/2/7.
//  Copyright © 2018年 init. All rights reserved.
//

import Foundation
import UserNotifications

class DPreferences{
    static let defaultInstance = DPreferences();
    class func defaultPreferences() -> DPreferences {
        return defaultInstance;
    }
    
    var doNotDisturb:Bool = false{
        didSet{
            if (doNotDisturb){
                UNUserNotificationCenter.current().removeAllDeliveredNotifications();
                UNUserNotificationCenter.current().removeAllPendingNotificationRequests();
            }else{
                DDroppingManager.sharedManager().startDoingDropNotification();
            }
            UserDefaults.standard.set(doNotDisturb, forKey: "not_disturb");
            UserDefaults.standard.synchronize();
        }
    }
    
    var notificationInterval:Int = 600{
        didSet{
            DDroppingManager.sharedManager().refreshDoingNotifications();
            UserDefaults.standard.set(notificationInterval, forKey: "notification_interval");
            UserDefaults.standard.synchronize();
        }
    }
    init() {
        let userDefault = UserDefaults.standard;
        if (userDefault.object(forKey: "not_disturb") != nil){
            self.doNotDisturb = userDefault.bool(forKey: "not_disturb")
        }
        if (userDefault.object(forKey: "notification_interval") != nil){
            self.notificationInterval = userDefault.integer(forKey: "notification_interval")
        }else{
            notificationInterval = 600;
        }
    }
    
    lazy var dateAnalysis:DAnalysisDatePrefItem = {
        let pref = DAnalysisDatePrefItem(title: "最近7天");
        pref.interval = .day;
        let predicate:NSPredicate = NSPredicate(format: "(begin >= %@)", Date().startDayDate(after: -(7 - 1))! as NSDate);
        pref.datePredicate = predicate;
        return pref;
    }();

    lazy var timelineAnalysis:DAnalysisDatePreference = {
        var analysis = DAnalysisDatePreference();
        analysis.intervalType = DAnalysisDatePreference.IntervalTypes.Week;
        return analysis;
    }();
}
